package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.model.partner.DeviceStatePeriod;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.utils.SiemensException;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceStatePeriodService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/11/3.
 */
@Controller
public class PartnerDeviceStatePeriodController {
    @Resource
    PartnerDeviceStatePeriodService partnerDeviceStatePeriodService;

    @Resource
    TenantService tenantService;

    /**
     * 时间轴数据
     *
     * @param tenantKey
     * @param startTime
     * @param deviceId
     * @return
     */
    @RequestMapping(value = "/partner/{tenant}/api/v1/device/periodsoftime", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPeriodsoftime(@PathVariable("tenant") String tenantKey,
                                                @RequestParam(value = "deviceId") String deviceId,
                                                @RequestParam(value = "startTime") String startTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            List<Map<String, Object>> mapList = partnerDeviceStatePeriodService.getPeriodsoftime(tenantId, deviceId, startTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(mapList).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_EVENT_NOT_EXISTED).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/device/periodsoftimeByCalculate", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPeriodsoftimeByCalculate(@PathVariable("tenant") String tenantKey,
                                                           @RequestParam(value = "deviceId") String deviceId,
                                                           @RequestParam(value = "startTime") String startTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            List<Map<String, Object>> mapList = partnerDeviceStatePeriodService.getPeriodsoftimeByCalculate(tenantId, deviceId, startTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(mapList).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_EVENT_NOT_EXISTED).build();
        }
    }

    //多台机床状态时间趋势
    @RequestMapping(value = "/partner/{tenant}/api/v1/device/mtStateMore", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDeviceStateChart(@RequestBody Map<String, Object> requestParam,
                                                   @PathVariable("tenant") String tenantKey) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            Map<String, List<Map<String, Object>>> map = partnerDeviceStatePeriodService.dataFormatOfMore(tenantId, (List<Map<String, Object>>) requestParam.get("deviceArr"), requestParam.get("startTime").toString(), requestParam.get("endTime").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //班次 时间段
    @RequestMapping(value = "/partner/{tenant}/api/v1/periodsoftime/shift", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPeriodstime(@PathVariable("tenant") String tenantKey,
                                              @RequestParam(name = "startTime") String startTime) {

        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> periodsoftimes = partnerDeviceStatePeriodService.getTimePeriod(tenantId, startTime);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(periodsoftimes).build();
    }

    //一天的状态
    @RequestMapping(value = "/partner/{tenant}/api/v1/periodsoftime/oneDayState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getOneDayState(@PathVariable("tenant") String tenantKey,
                                              @RequestParam(name = "deviceId") String deviceId,
                                              @RequestParam(name = "startTime") String startTime) {

        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> state = new ArrayList<>();
        try {
            state = partnerDeviceStatePeriodService.getOneDayState(tenantId, startTime, deviceId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(state).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(state).build();
        }
    }

    //某一班次或一天的状态
    @RequestMapping(value = "/partner/{tenant}/api/v1/periodStateTime/dayOrShiftState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getOneShiftState(@PathVariable("tenant") String tenantKey,
                                                @RequestParam(name = "deviceId") String deviceId,
                                                @RequestParam(value = "deviceName") String deviceName,
                                                @RequestParam(name = "day") String day,
                                                @RequestParam(name = "startTime") String startTime,
                                                @RequestParam(name = "endTime") String endTime) {

        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            List<Map<String, Object>> shiftStates = partnerDeviceStatePeriodService.getOneShiftState(tenantId, day, startTime, endTime, deviceId);

            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(shiftStates).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/periodStateTime/insert", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getOneShiftState(@PathVariable("tenant") String tenantKey,
                                                @RequestParam(name = "deviceId") String deviceId,
                                                @RequestParam(name = "startTime") String startTime,
                                                @RequestParam(name = "endTime") String endTime) {

        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        partnerDeviceStatePeriodService.saveDownTime(tenantId, startTime, endTime, deviceId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }
}
